package managedbean.FRM.CreditAccount;

import entity.CRM.Customer;
import entity.CRM.SaleLead;
import entity.CRM.SaleOrder;
import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.event.PhaseEvent;
import session.stateless.CRM.CustomerBeanLocal;
import session.stateless.CRM.SaleLeadBeanLocal;
import session.stateless.FRM.CreditAccountBeanLocal;


@ManagedBean(name="viewCreditAccountManagerBean")
@ViewScoped
public class ViewCreditAccountManagerBean implements Serializable {
    public ViewCreditAccountManagerBean() {}
    
    @EJB
    private CreditAccountBeanLocal creditAccountBeanLocal;
    @EJB
    private CustomerBeanLocal customerBeanLocal;
    @EJB
    private SaleLeadBeanLocal saleLeadBeanLocal;
    
    private Customer selectedCustomer;
    private String saleLeadName;
    
    public void initView(PhaseEvent event){
        List<String> temp = new ArrayList<String>();
        for (SaleLead temp_saleLead: saleLeadBeanLocal.getSalesLead())
            temp.add(temp_saleLead.getSaleLeadName());
        
        for (Customer temp_customer:customerBeanLocal.getCustomers())
        temp.add(temp_customer.getSaleLeadName());
        
        if (temp.contains(saleLeadName))
            this.setSelectedCustomer((Customer) saleLeadBeanLocal.findByName(saleLeadName));
    }
        

    public Customer getSelectedCustomer() {
        return selectedCustomer;
    }

    public void setSelectedCustomer(Customer selectedCustomer) {
        this.selectedCustomer = selectedCustomer;
    }

    public String getSaleLeadName() {
        return saleLeadName;
    }

    public void setSaleLeadName(String saleLeadName) {
        this.saleLeadName = saleLeadName;
    }
    
    
    public List<String> complete(String query)
    {
        List<String> results = new ArrayList<String>();  
        List<SaleLead> list = saleLeadBeanLocal.getSalesLead();
        List<Customer> list2 = customerBeanLocal.getCustomers();
        //System.out.println("test before adding "+list.size()+"      "+list2.size());
        for (int i = 0; i < list.size(); ++i)
        {
            if (list.get(i).getSaleLeadName().toLowerCase().contains(query.toLowerCase()))
                results.add(list.get(i).getSaleLeadName());
        }
        
        for (int i = 0; i < list2.size(); ++i)
        {
            if (list2.get(i).getSaleLeadName().toLowerCase().contains(query.toLowerCase()))
                results.add(list2.get(i).getSaleLeadName());
        }
        return results;          
    }
    
    public void retrieve()
    {
//        System.out.println("test"+selectedCustomer.getSaleLeadName());
//        this.setSelectedCustomer(selectedCustomer);
    }
    
    public void createDeposit(ActionEvent event) throws IOException
    {
        Customer selectedCustomer = this.getSelectedCustomer();        
        if (selectedCustomer != null){
            FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("selectedCustomer", selectedCustomer);
            FacesContext.getCurrentInstance().getExternalContext().redirect("../creditAccount/addDeposit.xhtml");                
        }
    }
    
    public void makeCashInAdvance(ActionEvent event) throws IOException
    {
        Customer selectedCustomer = this.getSelectedCustomer();        
        if (selectedCustomer != null){
            FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("selectedCustomer", selectedCustomer);
            FacesContext.getCurrentInstance().getExternalContext().redirect("../saleorder/viewCustomersSaleOrders.xhtml");                            
        }
    }
    
    public void payExistingInvoice(ActionEvent event) throws IOException
    {
        Customer selectedCustomer = this.getSelectedCustomer();        
        if (selectedCustomer != null){
            FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("selectedCustomer", selectedCustomer);
            FacesContext.getCurrentInstance().getExternalContext().redirect("../saleorder/viewCustomersSaleOrders.xhtml");                
        }
    }
    
    public void reduceOrder(ActionEvent event) throws IOException
    {
        Customer selectedCustomer = this.getSelectedCustomer();        
        if (selectedCustomer != null){
            FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("selectedCustomer", selectedCustomer);
            FacesContext.getCurrentInstance().getExternalContext().redirect("../saleorder/viewCustomersSaleOrders.xhtml");                
        }
    }
}
